package com.example.springboot.service.impl;

import com.example.springboot.pojo.Goods;
import com.example.springboot.pojo.User;
import com.example.springboot.service.CommentService;
import com.example.springboot.mapper.CommentMapper;
import com.example.springboot.pojo.Comment;
import com.example.springboot.utils.ThreadLocalUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public void create(Comment comment) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");
        comment.setUserId(userid);
        commentMapper.create(comment);
    }

    @Override
    public List<Comment> findAll() {
        return commentMapper.findAll();
    }


    @Override
    public List<Comment> findByUserName( String username) {
        return commentMapper.findByUserName(username);
    }

    @Override
    public void delete(Integer commentid) {
        commentMapper.delete(commentid);
    }

    @Override
    public PageInfo<Comment> selectPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Comment> list = commentMapper.findAll();
        return PageInfo.of(list);
    }
    @Override
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            commentMapper.delete(id);
        }
    }

    @Override
    public List<Comment> findByUserId() {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");
        return commentMapper.findByUserId(userid);
    }
}
